package com.chunfeng.lerunapplication.api;

import android.content.Context;
import com.chunfeng.lerunapplication.entity.JsonRequest;
import com.chunfeng.lerunapplication.entity.User;
import com.chunfeng.lerunapplication.utils.CacheUtils;
import com.chunfeng.lerunapplication.utils.HttpUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户接口
 *
 * @author by 春风能解释
 * <p>
 * 2023/11/27
 */

public class UserApi {

    /**
     * 应用上下文
     */
    private Context context;

    public UserApi(Context context) {
        this.context = context;
    }

    /**
     * 用户登录接口
     *
     * @param username 用户名
     * @param password 密码
     * @return JSON
     */
    public JsonRequest<String> login(String username, String password) {
        Map<String, Object> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        JsonRequest<String> post = new HttpUtils<String>()
                .sendHttpRequest(
                        ApiEnum.USER_LOGIN.getUrl(),
                        ApiEnum.USER_LOGIN.getMethod(),
                        null,
                        null,
                        params
                );
        if (post == null) {
            return null;
        }
        new CacheUtils(context).setValue("token", post.getData());
        return post;
    }

    /**
     * 用户注册接口
     *
     * @param username 用户名
     * @param password 密码
     * @return JSON
     */
    public JsonRequest<Integer> register(String username, String password) {
        Map<String, Object> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        return new HttpUtils<Integer>()
                .sendHttpRequest(
                        ApiEnum.USER_REGISTER.getUrl(),
                        ApiEnum.USER_REGISTER.getMethod(),
                        null,
                        null,
                        params
                );
    }

    /**
     * 查看当前用户
     *
     * @param userId 用户Id
     * @return JSON
     */
    public JsonRequest<User> lookCurrentUser(Integer userId) {
        Map<String, String> header = new HashMap<>();
        if (getToken(header)) return null;
        return new HttpUtils<User>()
                .sendHttpRequest(
                        ApiEnum.LOOK_CURRENT_USER.getUrl() + "/" + userId,
                        ApiEnum.LOOK_CURRENT_USER.getMethod(),
                        null,
                        header,
                        null
                );
    }

    /**
     * 获取Token
     *
     * @param header Map集合
     * @return 是否为空
     */
    private boolean getToken(Map<String, String> header) {
        String token = new CacheUtils(context).getValue("token");
        if (token.isEmpty()) {
            return true;
        }
        header.put("token", token);
        return false;
    }

    /**
     * 修改当前用户信息
     *
     * @param user 用户信息
     * @return JSON
     */
    public JsonRequest<Integer> updateCurrentUser(User user) {
        Map<String, String> header = new HashMap<>();
        if (getToken(header)) return null;
        return new HttpUtils<Integer>()
                .sendHttpRequest(
                        ApiEnum.UPDATE_USER.getUrl(),
                        ApiEnum.UPDATE_USER.getMethod(),
                        user,
                        header,
                        null
                );
    }

    /**
     * 头像下载
     *
     * @param id ID值
     * @return 文件流
     */
    public byte[] downloadAvatar(Integer id) {
        //请求头
        Map<String, String> header = new HashMap<>();
        if (getToken(header)) return null;
        header.put("Content-Type", "application/x-www-form-urlencoded");
        //参数
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        return new HttpUtils<>().getImage(
                ApiEnum.DOWNLOAD_AVATAR.getUrl(),
                ApiEnum.DOWNLOAD_AVATAR.getMethod(),
                null,
                header,
                params
        );
    }

    /**
     * 上传头像
     *
     * @param id   用户ID
     * @param file 文件对象
     * @return JSON
     */
    public JsonRequest<Integer> uploadAvatar(Integer id, File file) {
        //请求头
        Map<String, String> header = new HashMap<>();
        if (getToken(header)) return null;
        header.put("Content-Type", "image/jpeg");
        //参数
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        return new HttpUtils<Integer>().sendHttpRequest(
                ApiEnum.UPLOAD_AVATAR.getUrl(),
                ApiEnum.UPLOAD_AVATAR.getMethod(),
                file,
                header,
                params
        );
    }
}
